/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package multiThreadedressbook;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowSorter;
import javax.swing.border.BevelBorder;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author potirond
 */
public class FormAdd extends JFrame{
    
protected JButton submit = new JButton("Submit");
protected JTextField name = new JTextField(20);
protected JTextField firstname = new JTextField(20);
protected JTextField homeAddress = new JTextField(20);
protected JTextField compagnyAddress = new JTextField(20);
protected JTextField persMobileNum = new JTextField(20);
protected JTextField homeNum = new JTextField(20);
protected JTextField compagnyNum= new JTextField(20);
protected JTextField profMobileNum = new JTextField(20);
protected JTextField email = new JTextField(20);
protected JButton photo = new JButton("Click to search photo");
protected JComboBox groups = new JComboBox();
protected JTextField website = new JTextField();

protected JPanel mainPanel = new JPanel();
protected JPanel p = new JPanel();
protected JPanel list = new JPanel();
protected JPanel contactInfo = new JPanel();
protected JPanel group = new JPanel();
protected JScrollPane scrollPane = new JScrollPane();
protected JPanel research = new JPanel();
protected JPanel infoHead = new JPanel();
protected JPanel infoHeadEst = new JPanel();
protected JPanel infoBody = new JPanel();

protected JLabel newContact = new JLabel("New contact");
protected JLabel contactList = new JLabel("See contact list");
protected JButton delete = new JButton("Delete");
protected JButton modify = new JButton("Modify");
protected JMenuBar menuBar = new JMenuBar();
protected JMenu menuNewContact = new JMenu("New contact");
protected JMenu menuContactList = new JMenu("Contact list");
protected JButton all = new JButton("All contacts");
protected JButton famille = new JButton("Famille");
protected JButton amis = new JButton("Amis");
protected JButton collegues = new JButton("Collègues");
protected DefaultTableModel model;
protected JTable table;
protected JTextField fieldResearch=new JTextField(20);
protected JLabel lblResearch=new JLabel("Search Contact");
protected JLabel infoEmail=new JLabel();
protected JLabel infoWebsite=new JLabel();
    
    public FormAdd(){
        super();
        build();//On initialise notre fenêtre
    }

    public void build(){
                setTitle("My Address Book"); //On donne un titre à l'application
		setSize(900,400); //On donne une taille à notre fenêtre
		setLocationRelativeTo(null); //On centre la fenêtre sur l'écran
		setResizable(false); //On interdit la redimensionnement de la fenêtre
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //On dit à l'application de se fermer lors du clic sur la croix
                mainPanel.setLayout(null);
                contactInfo.setLayout(null);
                p.setLayout(new GridLayout(0,2));
                group.setLayout(null);
                list.setLayout(new BorderLayout());
                infoHead.setLayout(null);
                infoHeadEst.setLayout(new BoxLayout(infoHeadEst, BoxLayout.PAGE_AXIS));
                infoBody.setLayout(new GridLayout(0,2));

                JLabel lblNom = new JLabel("Name", JLabel.LEFT);
                lblNom.setLabelFor(name);       // Définir le composant qui es labelé
                JLabel lblFirstName = new JLabel("Firstname", JLabel.LEFT);
                lblNom.setLabelFor(firstname);
                JLabel lblHomeAddress = new JLabel("Home address", JLabel.LEFT);
                lblNom.setLabelFor(homeAddress);
                JLabel lblCompagnyAddress = new JLabel("Compagny address", JLabel.LEFT);
                lblNom.setLabelFor(compagnyAddress);
                JLabel lblPersMobileNum = new JLabel("Personal mobile", JLabel.LEFT);
                lblNom.setLabelFor(persMobileNum);
                JLabel lblHomeNum = new JLabel("Home telephone", JLabel.LEFT);
                lblNom.setLabelFor(homeNum);
                JLabel lblCompagnyNum = new JLabel("Compagny telephone", JLabel.LEFT);
                lblNom.setLabelFor(compagnyNum);
                JLabel lblProfMobileNum = new JLabel("Professional number", JLabel.LEFT);
                lblNom.setLabelFor(profMobileNum);
                JLabel lblEmail = new JLabel("Email", JLabel.LEFT);
                lblNom.setLabelFor(email);
                JLabel lblPhoto = new JLabel("Photo", JLabel.LEFT);
                lblNom.setLabelFor(photo);
                JLabel lblOther = new JLabel("Group", JLabel.LEFT);
                lblNom.setLabelFor(groups);
                JLabel lblSite = new JLabel("Web site", JLabel.LEFT);
                lblSite.setLabelFor(website);
                
                groups.addItem("famille");
                groups.addItem("amis");
                groups.addItem("collègues");
                p.add(lblNom);
                p.add(name);
                p.add(lblFirstName);
                p.add(firstname);
                p.add(lblHomeAddress);
                p.add(homeAddress);
                p.add(lblCompagnyAddress);
                p.add(compagnyAddress);
                p.add(lblPersMobileNum);
                p.add(persMobileNum);
                p.add(lblHomeNum);
                p.add(homeNum);
                p.add(lblCompagnyNum);
                p.add(compagnyNum);
                p.add(lblProfMobileNum);
                p.add(profMobileNum);
                p.add(lblEmail);
                p.add(email);
                p.add(lblPhoto);
                p.add(photo);
                p.add(lblOther);
                p.add(groups);
                p.add(lblSite);
                p.add(website);
                p.add(submit);
                
                //contactInfo.setBorder(BorderFactory.createLineBorder(Color.black));
                contactInfo.setBackground(Color.white);
                
                JPanel subPanel = new JPanel();
                subPanel.add(delete);
                subPanel.add(modify);
                research.add(lblResearch);
                research.add(fieldResearch);
                list.setBounds(130, 5, 445, 340);
                list.setBorder(new BevelBorder(BevelBorder.RAISED));
                list.add(research, BorderLayout.NORTH);
                list.add(subPanel, BorderLayout.SOUTH);
                //list.setBorder(BorderFactory.createLineBorder(Color.black));
                
                menuBar.add(menuNewContact);
                menuBar.add(menuContactList);
                
                
                group.setBounds(5,5,140,140);
                //group.setBorder(new BevelBorder(BevelBorder.RAISED));
                all.setBounds(0, 0, 120, 30);
                amis.setBounds(0, 30, 120, 30);
                famille.setBounds(0, 60, 120, 30);
                collegues.setBounds(0, 90, 120, 30);
                group.add(all);
                group.add(amis);
                group.add(famille);
                group.add(collegues);
                
                contactInfo.setBounds(580,5,310,340);
                mainPanel.add(list);
                mainPanel.add(contactInfo);
                mainPanel.add(group);
                
                this.setJMenuBar(menuBar);
                this.setContentPane(mainPanel);
                //setList();
                this.setVisible(true);
    }
    
    public void submit (ActionListener listener){
        submit.addActionListener(listener);
    }
    
    public void newContact (MouseListener listener){
        menuNewContact.addMouseListener(listener);
    }
    
    public void contactList (MouseListener listener){
        menuContactList.addMouseListener(listener);
    }
    
    public void delete (ActionListener listener){
        delete.addActionListener(listener);
    }
    
    public void setLabels(String name,String firstname,String homeAddress,String compagnyAddress,String persMobileNum,String homeNum,String officeNum,String profMobileNum,String email,String photo,String groups,String website) throws IOException{
        File f;
        Image img;
    try {
        f=new File(photo);
        img = ImageIO.read(f);
    } catch (IOException ex){ 
        f=new File("moi.png");
        img = ImageIO.read(f);
    }
        infoHead.setBounds(0, 0, 310, 150);
        Image resizedImg = img.getScaledInstance(150, 150, 0);
        JLabel image = new JLabel(new ImageIcon(resizedImg));
        image.setBounds(0, 0, 150, 150);
        infoHead.add(image);
        JLabel lblName = new JLabel(name);
        Font font=new Font("Arial",Font.BOLD,14);
        lblName.setFont(font);
        infoHeadEst.add(lblName);
        infoHeadEst.add(new JLabel(firstname));
        infoHeadEst.setBackground(Color.white);
        infoHeadEst.setBounds(160, 3, 100, 100);
        infoHead.add(infoHeadEst, BorderLayout.EAST);
        infoBody.add(new JLabel("Home address : "));
        infoBody.add(new JLabel(homeAddress));
        infoBody.add(new JLabel("Compagny address : "));
        infoBody.add(new JLabel(compagnyAddress));
        infoBody.add(new JLabel("Personal mobile : "));
        infoBody.add(new JLabel(persMobileNum));
        infoBody.add(new JLabel("Home number : "));
        infoBody.add(new JLabel(homeNum));
        infoBody.add(new JLabel("Office number : "));
        infoBody.add(new JLabel(officeNum));
        infoBody.add(new JLabel("Professional mobile : "));
        infoBody.add(new JLabel(profMobileNum));
        infoEmail.setText(email);
        infoEmail.setForeground(Color.blue);
        infoBody.add(new JLabel("Email : "));
        infoBody.add(infoEmail);
        infoBody.add(new JLabel("Group : "));
        infoBody.add(new JLabel(groups));
        infoWebsite.setText(website);
        infoWebsite.setForeground(Color.blue);
        infoBody.add(new JLabel("Web site : "));
        infoBody.add(infoWebsite);
        infoBody.setBackground(Color.white);
        infoHead.setBackground(Color.white);
        infoBody.setBounds(0, 153, 310, 190);
        contactInfo.add(infoHead, BorderLayout.NORTH);
        contactInfo.add(infoBody, BorderLayout.CENTER);
    }
    
    public void setTable(Object[][] data,String title[]){
        model = new DefaultTableModel(data, title);
        table = new JTable(model);
        RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
        table.setRowSorter(sorter);
        scrollPane.setBackground(Color.white);
        scrollPane.setViewportView(table);
        
        list.add(scrollPane, BorderLayout.CENTER);
        
    }
    
    public void contactInfo(ListSelectionListener listener){
            table.getSelectionModel().addListSelectionListener(listener);
    }
    
    public void photo(ActionListener listener){
        photo.addActionListener(listener);
    }
    
    public void amis(ActionListener listener){
        amis.addActionListener(listener);
    }
    
    public void all(ActionListener listener){
        all.addActionListener(listener);
    }
    
    public void famille(ActionListener listener){
        famille.addActionListener(listener);
    }
    
    public void collegues(ActionListener listener){
        collegues.addActionListener(listener);
    }
    
    public void modify (ActionListener listener){
        modify.addActionListener(listener);
    }
    
    public void launchMailBox (MouseListener listener){
        infoEmail.addMouseListener(listener);
    }
    
    public void launchBrowser (MouseListener listener){
        infoWebsite.addMouseListener(listener);
    }
    
    public void search (DocumentListener listener){
        fieldResearch.getDocument().addDocumentListener(listener);
    }
}
